<template>
  <menu
    id="menu"
    :style="{
      width: MenuWidth,
    }"
  >
    <div class="list-title">
      <span @click="expand" style="padding-left: 10px;">
        <i style="font-size: 16px;" :class="NavMenuIconStatus"></i>
      </span>
    </div>
    <ul class="list-panel" v-for="(item, index) of NavMenu" :key="index">
      <li
        class="list-item"
        :class="{
          'router-active': currentPath === item.path,
        }"
        @click="switchRouter(item, index)"
      >
        <i :class="item.icon"></i>
        <span class="list-item__text">{{ item.title }}</span>
        <img
          v-show="['美女', '跑车', '游戏'].includes(item.title)"
          style="margin-left: 110px;"
          :src="require('@/assets/img/hot.png')"
          width="20"
          height="auto"
        />
      </li>
    </ul>
    <div class="setting" @click="openDrawer">
      <i class="el-icon-setting"></i>
      <span>设置</span>
    </div>
    <el-drawer
      :visible="drawer"
      direction="rtl"
      @close="
        () => {
          this.drawer = false;
        }
      "
    >
      <template v-slot:title>
        <h3>设置</h3>
      </template>
      <setting @setted="setted"></setting>
    </el-drawer>
  </menu>
</template>

<script>
import Setting from "./setting.vue";

const NavMenuWidth = {
  collapse: "50px",
  expand: "220px",
};

const NavMenuIcon = {
  fold: "el-icon-s-fold",
  unfold: "el-icon-s-unfold",
};

export default {
  name: "nav-menu",
  data() {
    return {
      NavMenu: [],
      MenuWidth: NavMenuWidth.expand,
      NavMenuIconStatus: NavMenuIcon.fold,
      drawer: false,
    };
  },
  components: { Setting },

  computed: {
    currentPath() {
      return this.$route.path
    }
  },

  mounted() {
    this.NavMenu = this.$router.options.routes;
  },

  methods: {
    expand() {
      if (this.MenuWidth === NavMenuWidth.collapse) {
        this.MenuWidth = NavMenuWidth.expand;
        this.NavMenuIconStatus = NavMenuIcon.fold;
      } else {
        this.MenuWidth = NavMenuWidth.collapse;
        this.NavMenuIconStatus = NavMenuIcon.unfold;
      }
    },

    switchRouter(ele) {

      if (this.$route.path === ele.path) {
        return;
      }

      this.$router.push({
        path: ele.path,
        query: { type: ele._id },
      });
    },

    openDrawer() {
      this.drawer = true;
    },

    setted () {
      this.drawer = false;
    }
  },
};
</script>

<style lang="scss" scoped>
$height: 36px;
$line-height: 36px;

#menu {
  transition: width 0.3s linear;
  will-change: auto;
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  .list-title {
    height: $height;
    line-height: $line-height;
    font-size: 12px;
    padding: 0 0 0 10px;
  }
  .list-panel {
    transition: all 1s linear;
    max-height: 100px;
  }
  .list-item {
    height: $height;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-size: 14px;
    padding: 0 0 0 20px;
    white-space: nowrap;
    overflow: hidden;
    i {
      font-size: 16px;
    }
    &__text {
      margin-left: 16px;
    }
    &:hover {
      background-color: rgb(214, 215, 216);
      cursor: default;
    }
  }
  .router-active {
    background-color: var(--primary-color) !important;
    color: #ffffff;
  }

  .setting {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    font-size: 14px;
    padding: 10px 0 10px 20px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    i {
      margin-right: 16px;
    }
    &:hover {
      background-color: var(--primary-color);
      color: #ffffff;
    }
  }
}
</style>
